<template>
	<view>
		<view class="cell">
			<view class="label">手机号</view>
			<input type="number" maxlength="11" v-model="form.mobile" class="input" placeholder="请输入手机号" />
			
			<button @click="getCode" :disabled="codeDisabled" :style="codeDisabled?'':'background: #333;color: #fff;'"  class="btn-code">{{ codeText }}</button>
		</view>

		<view class="cell">
			<view class="label">验证码</view>
			<input v-model="form.code" maxlength="6" class="input" type="text" placeholder="请输入验证码" />
		</view>

		<!-- <view class="tips">需要先使用微信登录，在【我的】-【设置】中绑定手机号后，才能使用手机号登录</view> -->

		<view class="fixed-wrap">
			<button @click="login()" :disabled="loginDisabled"  :style="codeDisabled?'':'background: #333;color: #fff;'"  class="btn-login">确定</button>
		</view>
		
		<slideCode v-if="slideCode[0].show" :session_id="session_id[0]" :abcde='1' :phone="phone" @close="slideCode[0].show = false"
			@success="slideCode_success(0, $event)" ref="abcd"></slideCode>
	</view>
</template>

<script>
	import slideCode from '@/components/slideCode1.vue'
	import { isMobile } from '@/utils/validator.js';

	const SECONDS = 60;

	export default {
		components: {slideCode},
		data() {
			return {
				form: {
					mobile: '',
					code: '',
					invite_code:""
				},

				seconds: SECONDS,
				codeText: '获取验证码',
				counting: false,
				timer: null,
			
				codeStatus: true, //禁止重复点击
				invite_code: '', //邀请码
				
				
				seconds: 60,
				startText: '发送验证码',
				session_id: [], //会自动生成的
				slideCode: [{
					show: false
				}],
			};
		},
		onLoad() {
			if(uni.getStorageSync('inviteCode')) {
				this.invite_code = uni.getStorageSync('inviteCode');
			}
		},
		computed: {
			loginDisabled() {
				return !(this.form.mobile && this.form.code);
			},
			codeDisabled() {
				return !(this.form.mobile && !this.counting);
			}
		},
		methods: {
			// 登录
			loginHandle() {
			
			},

			// 获取验证码
			// getCode() {
			// 	if (!isMobile(this.form.mobile)) {
			// 		return uni.showToast({
			// 			title: '手机号码有误',
			// 			icon: 'none'
			// 		});
			// 	}
			// 	this.countdown();
			// },

			// 倒计时
			countdown() {
				this.counting = true;
				this.codeText = this.seconds + ' s';
				this.timer = setInterval(() => {
					if (this.seconds > 0) {
						this.seconds--;
						this.codeText = this.seconds + ' s';
					} else {
						this.counting = false;
						this.codeText = '再次获取';
						this.seconds = SECONDS;
						clearInterval(this.timer);
						this.timer = null;
					}
				}, 1000);
			},
			login() {
				if (!isMobile(this.form.mobile)) {
					return uni.showToast({
						title: '手机号码有误',
						icon: 'none'
					});
				}
				
				console.log(777777777)
				
				var _this = this;
				 if (!this.form.code) {
					uni.showToast({
						title: '请输入验证码',
						icon: 'none'
					})
				}else {
					this.$api.request('/api/user/mobilelogin_geng', this.form, '', function(res){
						console.log(res);
						if(res.code == 1) {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							})
						}else{
							uni.showToast({
								title: res.msg,
								icon: 'none'
							})
						}
					});
				}
			},
			
			getCode() {
				if (!isMobile(this.form.mobile)) {
					return uni.showToast({
						title: '手机号码有误',
						icon: 'none'
					});
				}
				
				// if (!isMobile(this.form.mobile)) {
				// 	return uni.showToast({
				// 		title: '手机号码有误',
				// 		icon: 'none'
				// 	});
				// }
				// this.countdown();
				this.phone = this.form.mobile
				this.session_id[0] = this.getRandomNunber(0,999999999);//唯一id
				this.slideCode[0].yanzheng = false;//图形验证码是否验证通过
				this.slideCode[0].x	= 0//图形验证码的水平偏移值
				// this.$slideCode_init(0);//初始化图形滑块
				this.slideCode[0].show = true
				this.slideCode[0].abcde = 1;
				this.slideCode[0].phone = this.form.mobile
				
				
				// if (this.$refs.uCode.canGetCode) {
				// 	// 模拟向后端请求验证码
				// 	uni.showLoading({
				// 		title: '正在获取验证码'
				// 	});
				// 	setTimeout(() => {
				// 		uni.hideLoading();
				// 		// 这里此提示会被this.start()方法中的提示覆盖
				// 		uni.$u.toast('验证码已发送');
				// 		// 通知验证码组件内部开始倒计时
				// 		this.$refs.uCode.start();
				// 	}, 2000);
				// } else {
				// 	uni.$u.toast('倒计时结束后再发送');
				// }
			},
			codeChange(text) {
				this.tips = text;
			},
			getRandomNunber (start,end) {
				return Math.floor(Math.random() * (end - start) + start)
			},
			slideCode_init (index) {
				this.session_id[index] = this.getRandomNunber(0,999999999);//唯一id
				this.slideCode[index].yanzheng = false;//图形验证码是否验证通过
				this.slideCode[index].x	= 0//图形验证码的水平偏移值
				this.slideCode[index].abcde = 1;
			},
			slideCode_success (index, data) {
				this.slideCode[index].yanzheng = true//验证通过
				this.slideCode[index].x	= data.x//偏移值
				this.slideCode[index].abcde = 1;
				this.yzm(index)//获取验证码
			},
			yzm(index) {
				
				let that = this;
				
				if (this.slideCode[index].yanzheng === true) { //图形滑块是否验证通过
					
				
						
						// 模拟向后端请求验证码
						uni.showLoading({
							title: '正在获取验证码'
						});
						
						setTimeout(() => {
							uni.hideLoading();
							// 这里此提示会被this.start()方法中的提示覆盖
							uni.$u.toast('验证码已发送');
							// 通知验证码组件内部开始倒计时
							that.countdown();
						}, 1000);
					
					
				} else {
					this.slideCode[index].show = true
				}
			}
			
			
			
			
			
			
			
			
		}
	};
</script>

<style lang="less">
	.cell {
		display: flex;
		align-items: center;
		padding: 16rpx 32rpx;
		border-bottom: 1px solid #f3f3f3;

		.label {
			font-size: 32rpx;
			font-weight: 600;
		}

		.input {
			flex: 1;
			height: 100rpx;
			padding: 0 32rpx;
		}

		.btn-code {
			font-size: 28rpx;
			background: #333;
			color: #fff;
			width: 200rpx;
		}

		.input-placeholder {
			color: #ccc;
		}
	}

	.tips {
		font-size: 26rpx;
		color: #999;
		padding: 64rpx 32rpx;
	}

	.fixed-wrap {
		position: fixed;
		left: 48rpx;
		right: 48rpx;
		bottom: 100rpx;
		.btn-login {
			background: #333;
			color: #fff;
			font-weight: bold;
			height: 100rpx;
			line-height: 100rpx;
		}
	}
	button .btn-login{
		background: red;
	}
	button[disabled].btn-login {
		background: #e6e6e6;
	}

	button[disabled].btn-code {
		background: #e6e6e6;
	}


		
	button::after {
		border: none;
	}
</style>
